package product

import (
	"context"
	"devops-super/api/product/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.GetPageLstReq) (res *v1.GetPageLstRes, err error) {
	res = &v1.GetPageLstRes{}
	res.PageLstRes, err = service.Product().GetPageLst(ctx, req)
	return
}

func (*ControllerV1) Add(ctx context.Context, req *v1.AddReq) (res *v1.AddRes, err error) {
	product := &entity.AppProduct{
		ProductKey:  req.ProductKey,
		Name:        req.Name,
		Type:        req.Type,
		OpenStatus:  req.OpenStatus,
		Remark:      req.Remark,
		Description: req.Description,
		Version:     req.Version,
		Image:       req.Image,
		CreatedAt:   gtime.Now(),
	}
	err = service.Product().Add(ctx, product)
	return
}

func (*ControllerV1) Upt(ctx context.Context, req *v1.UptReq) (res *v1.UptRes, err error) {
	product := &entity.AppProduct{
		Id:          uint64(req.Id),
		ProductKey:  req.ProductKey,
		Name:        req.Name,
		Type:        req.Type,
		OpenStatus:  req.OpenStatus,
		Remark:      req.Remark,
		Description: req.Description,
		Version:     req.Version,
		Image:       req.Image,
		UpdatedAt:   gtime.Now(),
	}
	err = service.Product().Upt(ctx, product)
	return
}

func (*ControllerV1) UptStatus(ctx context.Context, req *v1.UptStatusReq) (res *v1.UptStatusRes, err error) {
	err = service.Product().UptStatus(ctx, req.Id, req.OpenStatus)
	return
}

func (*ControllerV1) Del(ctx context.Context, req *v1.DelReq) (res *v1.DelRes, err error) {
	err = service.Product().Del(ctx, req.Id)
	return
}

func (*ControllerV1) GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error) {
	res1, err := service.Product().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = new(v1.GetOneRes)
	res.Info = res1
	return
}

func (*ControllerV1) GetOptions(ctx context.Context, req *v1.GetOptionsReq) (res *v1.GetOptionsRes, err error) {
	res, err = service.Product().GetOptions(ctx, req)
	return
}
